package com.ctrl.riveradministrator.mvp.view.activity.problem_list;

import android.content.Context;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.ctrl.riveradministrator.R;
import com.ctrl.riveradministrator.base.BaseActivity;
import com.ctrl.riveradministrator.mvp.model.Problem;
import com.ctrl.riveradministrator.mvp.presenter.ProblemListPresenter;
import com.ctrl.riveradministrator.mvp.presenter.ProblemReportPresenter;
import com.ctrl.riveradministrator.mvp.view.activity.problem_report.ProblemReportView;

import java.util.ArrayList;
import java.util.List;

public class AssignmentiActivity extends BaseActivity<ProblemReportPresenter> implements ProblemReportView {

    private Toolbar toolbar;
    private TextView titleTv;
    private ImageView leftMenu;
    private Spinner person;
    private Button submit;
    private ProgressBar bar;
    private String personId = "-1";

    /**
     * 初始化bundle参数
     *
     * @param parms parms
     */
    @Override
    protected void initParams(Bundle parms) {

    }

    /**
     * 绑定布局
     *
     * @return 布局ID
     */
    @Override
    protected int bindLayout() {
        return R.layout.activity_assignmenti;
    }

    /**
     * 初始化控件
     *
     * @param view view
     */
    @Override
    protected void initView(View view) {
        toolbar = (Toolbar) $(R.id.toolbar);
        titleTv = (TextView) $(R.id.title_tv);
        leftMenu = (ImageView) $(R.id.left_iv);
        submit = (Button) $(R.id.submit_btn);
        person = $(R.id.person);
        bar = $(R.id.probar);
    }

    /**
     * 业务操作
     *
     * @param mContext context
     */
    @Override
    protected void doBusiness(Context mContext) {
        bar.setVisibility(View.VISIBLE);
        mPresenter.getPerson();
    }

    /**
     * View点击
     *
     * @param view view
     */
    @Override
    protected void widgetClick(View view) {

    }

    /**
     * 设置监听
     */
    @Override
    protected void setListener() {
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.assignment(personId, getIntent().getStringExtra("problemId"));
            }
        });
    }

    @Override
    public void handleMessage(Message message, int what) {

    }

    @Override
    protected ProblemReportPresenter createPresenter() {
        return new ProblemReportPresenter(this);
    }

    @Override
    public void showMessage() {
        Toast.makeText(this, "指派成功", Toast.LENGTH_SHORT).show();
        bar.setVisibility(View.GONE);
        finish();
    }

    @Override
    public void showMessage(List<Problem> problem) {
        try {
            List<String> name = new ArrayList<>();
            for (int i = 0; i < problem.size(); i++) {
                name.add(problem.get(i).getUserName());
            }
            personId = problem.get(0).getUserId();
            ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, name);
            person.setAdapter(adapter);
            person.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    personId = problem.get(position).getUserId();
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        bar.setVisibility(View.GONE);
    }

    @Override
    public void showError(String msg) {
        bar.setVisibility(View.GONE);
        Toast.makeText(this, "网络连接错误:" + msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void uploading(String percent) {

    }
}